home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib / interfaces.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  6.2 KB  |  185 lines

  1.  
  2. try:
  3.     from zope.interface import Interface, classImplements, implements
  4. except ImportError:
  5.     class Interface(object): pass
  6.     def classImplements(c, i): pass
  7.     def implements(*args): pass
  8.  
  9. from rdflib import RDF
  10.  
  11. class IGraph(Interface):
  12.     """\
  13.     An rdflib.Graph indexes data expressed in the Resource Description
  14.     Framework (RDF).  Any kind of content, whether inside Zope or from
  15.     some outside source, can be cataloged if it can describe itself
  16.     using the RDF standard.  Any kind of RDF vocabulary like RSS, OWL,
  17.     DAML+OIL, Dublin Core, or any kind of XML schema or data can be
  18.     expressed into the graph.
  19.  
  20.     Once data is graphed it can be queried using either the Python
  21.     query interface, a TALES-based RDF query expression language, or
  22.     the sparql rdf query language.  Results of a query can be either a
  23.     generator of result records or RDF in xml or NT format.
  24.  
  25.     In Semantic Web terms, a graph is a persistent triple store.  RDF
  26.     is broken down into subject, predicate, and object relations
  27.     (called triples) and each relation is indexed.  The triple store
  28.     can then be queried for triples that match patterns.
  29.     """
  30.  
  31.     def parse(rdf, format="xml"):
  32.         """ Parse RDF-XML into the catalog. """
  33.  
  34.     def add((subject, predicate, object)):
  35.         """ Add one triple to the catalog.  """
  36.  
  37.     def remove((subject, predicate, object)):
  38.         """ Remove one triple from the catalog. """
  39.  
  40.     def triples((subject, predicate, object), *args):
  41.         """ Query the triple store. """
  42.  
  43.     def contexts(triple=None):
  44.         """ Generator over all contexts in the graph. If triple is
  45.         specified, a generator over all contexts the triple is in."""
  46.  
  47.     def value(subject, predicate=RDF.value, object=None, default=None, any=False):
  48.         """ Get a value for a subject/predicate, predicate/object, or
  49.         subject/object pair -- exactly one of subject, predicate,
  50.         object must be None. Useful if one knows that there may only
  51.         be one value.
  52.  
  53.         It is one of those situations that occur a lot, hence this
  54.         'macro' like utility
  55.  
  56.         Parameters:
  57.         -----------
  58.         subject, predicate, object  -- exactly one must be None
  59.         default -- value to be returned if no values found
  60.         any -- if True:
  61.                  return any value in the case there is more than one
  62.                else:
  63.                  raise UniquenessError
  64.         """
  65.  
  66.     def label(subject, default=''):
  67.         """ Queries for the RDFS.label of the subject, returns default
  68.         if no label exists."""
  69.  
  70.     def comment(subject, default=''):
  71.         """ Queries for the RDFS.comment of the subject, returns
  72.         default if no comment exists."""
  73.  
  74.     def items(list):
  75.         """Generator over all items in the resource specified by list
  76.         (an RDF collection)"""
  77.  
  78.     def __iter__():
  79.         """ Iterates over all triples in the store."""
  80.  
  81.     def __contains__(triple):
  82.         """ Support for 'triple in graph' syntax."""
  83.  
  84.     def __len__(context=None):
  85.         """ Returns the number of triples in the graph. If context is
  86.         specified then the number of triples in the context is
  87.         returned instead."""
  88.  
  89.     def __eq__(other):
  90.         """ Test if Graph is exactly equal to Graph other."""
  91.  
  92.     def __iadd__(other):
  93.         """ Add all triples in Graph other to Graph."""
  94.  
  95.     def __isub__(other):
  96.         """ Subtract all triples in Graph other from Graph."""
  97.  
  98.     def subjects(predicate=None, object=None):
  99.         """ A generator of subjects with the given predicate and
  100.         object."""
  101.  
  102.     def predicates(subject=None, object=None):
  103.         """ A generator of predicates with the given subject and
  104.         object."""
  105.  
  106.     def objects(subject=None, predicate=None):
  107.         """ A generator of objects with the given subject and
  108.         predicate."""
  109.  
  110.     def subject_predicates(object=None):
  111.         """ A generator of (subject, predicate) tuples for the given
  112.         object"""
  113.  
  114.     def subject_objects(predicate=None):
  115.         """ A generator of (subject, object) tuples for the given
  116.         predicate"""
  117.  
  118.     def predicate_objects(subject=None):
  119.         """ A generator of (predicate, object) tuples for the given
  120.         subject"""
  121.  
  122.     def get_context(identifier):
  123.         """ Returns a Context graph for the given identifier, which
  124.         must be a URIRef or BNode."""
  125.  
  126.     def remove_context(identifier):
  127.         """ Removes the given context from the graph. """
  128.  
  129.     def transitive_objects(subject, property, remember=None):
  130.         """ """
  131.  
  132.     def transitive_subjects(predicate, object, remember=None):
  133.         """ """
  134.  
  135.     def load(location, publicID=None, format="xml"):
  136.         """ for b/w compat. See parse."""
  137.  
  138.     def save(location, format="xml", base=None, encoding=None):
  139.         """ for b/x compat. See serialize."""
  140.  
  141.     def context_id(uri):
  142.         pass
  143.  
  144.     def parse(source, publicID=None, format="xml"):
  145.         """ Parse source into Graph. If Graph is context-aware it'll
  146.         get loaded into it's own context (sub graph). Format defaults
  147.         to xml (AKA rdf/xml). The publicID argument is for specifying
  148.         the logical URI for the case that it's different from the
  149.         physical source URI. Returns the context into which the source
  150.         was parsed."""
  151.  
  152.     def serialize(destination=None, format="xml", base=None, encoding=None):
  153.         """ Serialize the Graph to destination. If destination is None
  154.         serialize method returns the serialization as a string. Format
  155.         defaults to xml (AKA rdf/xml)."""
  156.  
  157.     def seq(subject):
  158.         """
  159.         Check if subject is an rdf:Seq. If yes, it returns a Seq
  160.         class instance, None otherwise.
  161.         """
  162.  
  163.     def absolutize(uri, defrag=1):
  164.         """ Will turn uri into an absolute URI if it's not one already. """
  165.  
  166.     def bind(prefix, namespace, override=True):
  167.         """Bind prefix to namespace. If override is True will bind
  168.         namespace to given prefix if namespace was already bound to a
  169.         different prefix."""
  170.  
  171.     def namespaces():
  172.         """Generator over all the prefix, namespace tuples.
  173.         """
  174.  
  175. class IIdentifier(Interface):
  176.  
  177.     def n3():
  178.         """ Return N3 representation of identifier. """
  179.  
  180.     def startswith(string):
  181.         """ dummy. """
  182.  
  183.     def __cmp__(other):
  184.         """ dummy. """
  185.